
Imports System.Data.SqlClient
Imports System.Configuration



Public Enum DocumentType
    Page
    Content
    DailyArticle
    Product
    Calendar
    Media
    PDF
End Enum


Public Class Document
    Inherits BusinessBase


    Private mIdentifier As Guid
    Private mTitle As String
    Private mDescription As String
    Private mDocumentType As DocumentType

#Region "Properties"

    Public Id As Integer
    Public Contents As String
    Public Keywords As String
    Public CreateDate As Date

    Protected PreviousGUID As String
    Protected NextGUID As String


    Public Sub SetDocumentType(ByVal type As String)
        Select Case type
            Case "Page"
                Me.DocumentType = TagLib.DocumentType.Page
            Case "Content"
                Me.DocumentType = TagLib.DocumentType.Content
            Case "DailyArticle"
                Me.DocumentType = TagLib.DocumentType.DailyArticle
            Case "Product"
                Me.DocumentType = TagLib.DocumentType.Product
            Case "Calendar"
                Me.DocumentType = TagLib.DocumentType.Calendar
            Case "Media"
                Me.DocumentType = TagLib.DocumentType.Media
            Case "PDF"
                Me.DocumentType = TagLib.DocumentType.PDF
            Case Else
                Me.DocumentType = TagLib.DocumentType.Content
        End Select
    End Sub

    Public Property DocumentType() As DocumentType
        Get
            Return mDocumentType
        End Get
        Set(ByVal value As DocumentType)
            mDocumentType = value
        End Set
    End Property
    Public ReadOnly Property URL() As String
        Get
            If Me.Id > 0 Then
                Select Case DocumentType
                    Case TagLib.DocumentType.DailyArticle
                        Return String.Format("/story/{0}", (Me.Id))
                    Case TagLib.DocumentType.Product
                        Return String.Format("/store/Product1.aspx?Product_Id={0}", (Me.Id))
                    Case TagLib.DocumentType.Calendar
                        Return String.Format("/event/{0}", (Me.Id))
                    Case Else
                        Return String.Format("/resources/{0}", Identifier)
                End Select
            Else
                Return String.Format("/resources/{0}", Identifier)
            End If
        End Get
    End Property


    Public Property Title() As String
        Get
            Return mTitle
        End Get
        Set(ByVal value As String)
            mTitle = value
        End Set
    End Property

    Public Property Identifier() As Guid
        Get
            Return mIdentifier
        End Get
        Set(ByVal value As Guid)
            mIdentifier = value
        End Set
    End Property

    Public Overridable ReadOnly Property NextURL() As String
        Get
            If NextGUID <> "" Then
                Return String.Format("/resources/{0}", NextGUID)
            End If
            Return ""
        End Get
    End Property

    Public Overridable ReadOnly Property PreviousURL() As String
        Get
            If PreviousGUID <> "" Then
                Return String.Format("/resources/{0}", PreviousGUID)
            End If
            Return ""
        End Get
    End Property

    Public Property Description() As String
        Get
            Return mDescription
        End Get
        Set(ByVal value As String)
            mDescription = value
        End Set
    End Property

#End Region


    Public Sub New()
    End Sub


    Public Sub New(ByVal Identifier As Guid)

        Using _
            contentReader As SqlDataReader = _
                SqlHelper.ExecuteReader(BusinessBase.ConnectionString, _
                                         CommandType.StoredProcedure, "GetContentbyGUID", _
                                         New SqlParameter("GUID", Identifier))
            While contentReader.Read()
                Id = CInt(contentReader("Id"))
                Me.Identifier = Identifier
                Me.Title = ObjToString(contentReader("Title"))
                Me.Description = ObjToString(contentReader("Description"))
                Me.Contents = ObjToString(contentReader("Contents"))
                Me.Keywords = ObjToString(contentReader("Keywords"))
                If IsDBNull((contentReader("CreateDate"))) Then Me.CreateDate = DirectCast(contentReader("CreateDate"), Date)
                Me.SetDocumentType(contentReader("DocumentType"))

                Try
                    Me.PreviousGUID = ObjToString(contentReader("PreviousGUID"))
                    Me.NextGUID = ObjToString(contentReader("NextGUID"))
                Catch
                End Try


            End While
        End Using
    End Sub


    Public Shared Function Delete(ByVal Identifier As Guid) As Integer
        Return _
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "PageDelete", _
                                       New SqlParameter("GUID", Identifier))
    End Function


#Region "Browsing"


    Public Shared Function GetPageGUID(ByVal Page As String, ByVal Path As String) As Guid
        Dim _
            value As String = _
                ObjToString( _
                             SqlHelper.ExecuteScalar(ConnectionString, _
                                                      CommandType.StoredProcedure, "PagesGetGUID", _
                                                      New SqlParameter("@PageName", Page), _
                                                      New SqlParameter("@Path", Path)))
        If value <> "" Then
            Return New Guid(value)
        Else
            Return Guid.Empty
        End If
    End Function

    ''' <summary>
    ''' Gets the page id from the GUID.
    ''' </summary>
    ''' <param name="Ident">The ident.</param>
    ''' <returns></returns>
    Public Shared Function GetPageIdfromGUID(ByVal Ident As Guid) As Integer
        Return _
            ObjToInteger( _
                          SqlHelper.ExecuteScalar(ConnectionString, CommandType.Text, _
                                                   "select PageId from Page WHERE GUID =@GUID", _
                                                   New SqlParameter("@GUID", Ident.ToString())))
    End Function

    ''' <summary>
    ''' Gets the GUID from the Page Id.
    ''' </summary>
    ''' <param name="PageId">The page id.</param>
    ''' <returns></returns>
    Public Shared Function GetGUID(ByVal PageId As Integer) As Guid
        Dim _
            value As String = _
                ObjToString( _
                             SqlHelper.ExecuteScalar(ConnectionString, CommandType.Text, _
                                                      "select GUID from Page WHERE PageId = @PageId", _
                                                      New SqlParameter("@PageId", PageId)))

        If value <> "" Then
            Return New Guid(value)
        Else
            Return Guid.Empty
        End If
    End Function

    Public Shared Function GetNextId(ByVal PageId As Integer) As Int32
        Try
            Return _
                Convert.ToInt32( _
                                 SqlHelper.ExecuteScalar(ConnectionString, CommandType.Text, _
                                                          "select top 1 PageId from Page where PageId > @PageId", _
                                                          New SqlParameter("@PageId", PageId)).ToString())
        Catch

            Return _
                Convert.ToInt32( _
                                 SqlHelper.ExecuteScalar(ConnectionString, CommandType.Text, _
                                                          "select top 1 PageId from Page ORDER BY PageId DESC"))
        End Try

    End Function

    Public Shared Function GetPreviousId(ByVal PageId As Integer) As Int32
        Try
            Return _
                Convert.ToInt32( _
                                 SqlHelper.ExecuteScalar(ConnectionString, CommandType.Text, _
                                                          "select top 1 PageId from Page where PageId < @PageId ORDER BY PageId DESC", _
                                                          New SqlParameter("@PageId", PageId)).ToString())
        Catch

            Return _
                Convert.ToInt32( _
                                 SqlHelper.ExecuteScalar(ConnectionString, CommandType.Text, _
                                                          "select top 1 PageId from Page ORDER BY PageId "))
        End Try
    End Function


#End Region
End Class

